//
//  imageTableViewCell.m
//  糗百——王桂敏
//
//  Created by 王桂敏 on 15/5/28.
//  Copyright (c) 2015年 lanouhn. All rights reserved.
//

#import "TableViewCell.h"
#import "UIImageView+WebCache.h"
#import "UMSocial.h"
#import "Define.h"
@interface TableViewCell ()

@end

@implementation TableViewCell

- (void)dealloc{
    [_icon release];
    [_userName release];
    [_commentButton release];
    [_contentLable release];
    [_forButton release];
    [_againstButton release];
    [_favButton release];
    [_stateLable release];
    [_playButton release];
    [_picImageView release];
    [_image release];
//    [_movie release];
    [_user_id release];
    [_userButton release];
    [super dealloc];
}
#pragma mark - laze loading -
//- (MoviePlayer *)movie{
//    if (!_movie) {
//        self.movie = [[MoviePlayer alloc] init];
//        [self.contentView addSubview:_movie.view];
//    }
//    return _movie;
//}
- (UIImageView *)picImageView{
    if (!_picImageView) {
        self.picImageView = [[UIImageView alloc] init];
        _picImageView.userInteractionEnabled = YES;
    }
    return _picImageView;
}
- (UIButton *)playButton{
    if (!_playButton) {
        self.playButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _playButton.frame = CGRectMake(0, 0, 50, 50);
        [_playButton setImage:[UIImage imageNamed:@"视频播放按钮.jpg"] forState:UIControlStateNormal];
        _playButton.layer.cornerRadius = 25;
        _playButton.layer.masksToBounds = YES;
        _playButton.alpha = 0.8;

    }
    return _playButton;
}
- (UIImageView *)image{
    if (!_image) {
        self.image = [[UIImageView alloc] init];
        [self.contentView addSubview:_image];
    }
    return _image;
}

- (UIImageView *)icon{
    if (!_icon) {
        self.icon = [[[UIImageView alloc] initWithFrame:CGRectMake(10, 9, 30, 30)] autorelease];
        _icon.layer.cornerRadius = 15;
        _icon.layer.masksToBounds = YES;
        _icon.userInteractionEnabled = YES;
            }
    return _icon;
}
- (UIButton *)userButton{
    if (!_userButton) {
        self.userButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _userButton.frame = CGRectMake(0, 0, 30, 30);
        _userButton.layer.cornerRadius = 15;
        _userButton.layer.masksToBounds = YES;
        [self.icon addSubview:_userButton];

    }
    return _userButton;
}

- (UILabel *)userName{
    if (!_userName) {
        self.userName = [[[UILabel alloc] initWithFrame:CGRectMake(45, 9, 200, 30)] autorelease];
        _userName.font = [UIFont systemFontOfSize:12];
        _userName.textColor = [UIColor orangeColor];
    }
    return _userName;
}
- (UILabel *)contentLable{
    if (!_contentLable) {
        self.contentLable = [[[UILabel alloc] init] autorelease];
        _contentLable.font = [UIFont systemFontOfSize:13];
        _contentLable.textColor = [UIColor grayColor];
        _contentLable.numberOfLines = 0;
    }
    return _contentLable;
}
- (UIButton *)forButton{
    if (!_forButton) {
        self.forButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_forButton setImage:[UIImage imageNamed:@"icon_for"] forState:UIControlStateNormal];
        [_forButton setImage:[UIImage imageNamed:@"icon_for_active"] forState:UIControlStateHighlighted];
    }
    return _forButton;
}

- (UIButton *)againstButton{
    if (!_againstButton) {
        self.againstButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_againstButton setImage:[UIImage imageNamed:@"icon_against"] forState:UIControlStateNormal];
        [_againstButton setImage:[UIImage imageNamed:@"icon_against_active"] forState:UIControlStateHighlighted];

    }
    return _againstButton;
}

- (UIButton *)commentButton{
    if (!_commentButton) {
        self.commentButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_commentButton setImage:[UIImage imageNamed:@"button_comment"] forState:UIControlStateNormal];
    }
    return _commentButton;
}

- (UIButton *)favButton{
    if (!_favButton) {
        self.favButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_favButton setImage:[UIImage imageNamed:@"icon_fav"] forState:UIControlStateNormal];
        [_favButton setImage:[UIImage imageNamed:@"icon_fav_active"] forState:UIControlStateHighlighted];
    }
    return _favButton;
}

- (UILabel *)stateLable{
    if (!_stateLable) {
        self.stateLable = [[UILabel alloc] init];
        _stateLable.font = [UIFont systemFontOfSize:10];
        _stateLable.textColor = [UIColor lightGrayColor];
    }
    return _stateLable;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self.contentView addSubview:self.icon];
        [self.contentView addSubview:self.userName];
        [self.contentView addSubview:self.contentLable];
        [self.contentView addSubview:self.stateLable];
        [self.contentView addSubview:self.forButton];
        [self.contentView addSubview:self.againstButton];
        [self.contentView addSubview:self.favButton];
        [self.contentView addSubview:self.commentButton];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;

}

- (void)configureWithMessage:(MessageModel *)message{
//---------------------------用户信息----------------------------
    self.user_id = message.user.uid;
    //当用户信息存在时，设置用户名和用户头像
    if (message.user) {
        self.userName.text = message.user.login;//设置用户名
        if ([message.user.icon isKindOfClass:[NSNull class]]) {
            //当用户头像不存在时，设置用户头像为匿名头像
            self.icon.image = [UIImage imageNamed:@"user_icon_anonymous"];
        } else {
            //当用户头像存在，则下载头像图片并使用
            NSURL *iconURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://pic.qiushibaike.com/system/avtnew/%@/%@/medium/%@",[[NSString stringWithFormat:@"%@",message.user.uid] substringWithRange:NSMakeRange(0, 4)],message.user.uid,message.user.icon]];
            [self.icon sd_setImageWithURL:iconURL];
        }
    } else {
        //当用户信息不存在时，用户信息设置为匿名用户
        self.userName.text = @"匿名人士";
        self.icon.image = [UIImage imageNamed:@"user_icon_anonymous"];
    }
//------------------------------图片信息----------------------------------
    //获取图片的URL
    NSURL *imageURL = [NSURL URLWithString:[NSString stringWithFormat:@"http://pic.qiushibaike.com/system/pictures/%@/%@/medium/%@",[[NSString stringWithFormat:@"%@",message.message_id] substringWithRange:NSMakeRange(0, 5)],message.message_id,message.image]];
    //设置图片的初始宽度为0
    CGFloat imageHeight = 0;

    if (![message.image_size[@"s"] isKindOfClass:[NSNull class]] && [[message.image_size[@"s"] firstObject] floatValue] != 0) {
        //当图片的尺寸信息存在且图片的宽度不为0，则在保持图片原有比例情况下算出适合屏幕宽度的尺寸
        self.image.frame = CGRectMake(10,kBUTTON_Y - 20 , kSCREENWIDTH - 20, [message.image_size[@"s"][1] floatValue] / [[message.image_size[@"s"] firstObject] floatValue] * (kSCREENWIDTH - 20));
        //图片存在时，设置图片的宽度
        imageHeight = [message.image_size[@"s"][1] floatValue] / [[message.image_size[@"s"] firstObject] floatValue] * (kSCREENWIDTH - 20);
    }

    [self.image sd_setImageWithURL:imageURL];//获取图片

//----------------------------配置视频信息----------------------------
    CGFloat movieHeight = 0;//设置视频的默认高度为0
    self.picImageView.frame = CGRectZero;
    self.playButton.frame = CGRectZero;
    MoviePlayer *player = [MoviePlayer defaultMoviePlayer];
    [player.view removeFromSuperview];

    player.controlStyle = MPMovieControlStyleDefault;
    player.initialPlaybackTime = 0;

    if (message.low_url) {
        self.picImageView.frame = CGRectMake(10, message.contentRect.size.height + 48 + 10, kSCREENWIDTH - 20, kSCREENWIDTH - 20);
         self.playButton.frame = CGRectMake(0, 0, 50, 50);
        NSLog(@"%@",message.pic_url);
        if (message.pic_url) {

            [self.picImageView sd_setImageWithURL:[NSURL URLWithString:message.pic_url] placeholderImage:[UIImage imageNamed:@"anyUser.jpg"]];
        } else {
            self.picImageView.image = [UIImage imageNamed:@"anyUser.jpg"];
        }
        [self.contentView addSubview:self.picImageView];

        [self.contentView addSubview:self.playButton];

        self.playButton.center = self.picImageView.center;
        [self.playButton addTarget:self action:@selector(handlePlayButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        self.playButton.titleLabel.text = message.low_url;

        [player.view setFrame:CGRectMake(10, message.contentRect.size.height + 48 + 10, kSCREENWIDTH - 20, kSCREENWIDTH - 20)];


        movieHeight = kSCREENWIDTH - 20;


    }


    self.contentLable.frame = CGRectMake(10, 48, kSCREENWIDTH - 20, message.contentRect.size.height);
    self.contentLable.text = message.content;
    self.stateLable.frame = CGRectMake(15, message.contentRect.size.height + imageHeight + movieHeight + 48 + 10, kSCREENWIDTH - 25, 20);
    int votes = [message.votes[@"up"] intValue] - [message.votes[@"down"] intValue];
    self.stateLable.text = [NSString stringWithFormat:@"好笑 %d · 评论 %@",votes,message.comments_count];
    self.forButton.frame = CGRectMake(0, kBUTTON_Y + imageHeight + movieHeight, 156 / 3, 98 / 3);
    
    self.againstButton.frame = CGRectMake(156 / 3, kBUTTON_Y + imageHeight  + movieHeight, 129 / 3, 98 / 3);
    self.commentButton.frame = CGRectMake(156 / 3 + 129 / 3, kBUTTON_Y + imageHeight + movieHeight, 151 / 3, 98 / 3);
    self.favButton.frame = CGRectMake(kSCREENWIDTH - 133 / 3, kBUTTON_Y + imageHeight + movieHeight, 133 / 3, 98 / 3);
}


- (void)handlePlayButtonAction:(UIButton *)sender{

     MoviePlayer *player = [MoviePlayer defaultMoviePlayer];
//    [player.view removeFromSuperview];
    player.contentURL = [NSURL URLWithString:sender.titleLabel.text];

    [self.contentView addSubview:player.view];
        [player play];

}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/


@end
